﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace ZHDJ.MobileInterface
{
    public partial class SMSAccept : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            string result = "";
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            string strOriRequestMsg = "";
            try
            {
                strOriRequestMsg = RequestHelper.ReadRequestMessage(Request);
                if (string.IsNullOrEmpty(strOriRequestMsg))
                { throw new Exception("没有获取到短信内容"); }

                string[] notes = strOriRequestMsg.Split(';');
                foreach (string note in notes)
                {
                    string[] noteDetail = note.Split(',');
                    if (noteDetail == null || noteDetail.Length != 7)
                    {
                        CommonUtils.InsertMobileInterfaceLog("SMSAccept", strOriRequestMsg, "短信解析失败:" + note);
                        continue;
                    }

                    //mo ID，特服号，手机号，内容（对内容进行gb2312解码），时间
                    PartyMoSmsReceive smsReceive = new PartyMoSmsReceive();
                    smsReceive.Mobile = noteDetail[2];
                    smsReceive.MoID = noteDetail[0];
                    smsReceive.RecContent = HttpUtility.UrlDecode(noteDetail[3], Encoding.GetEncoding("gb2312"));
                    smsReceive.RecTime = noteDetail[4];
                    smsReceive.SendDetailID = "";
                    smsReceive.ServiceNo = noteDetail[1];
                    if (noteDetail[5] != "ZHDJ_XYDXDXHF_150330")
                    {
                        CommonUtils.InsertMobileInterfaceLog("SMSAccept", strOriRequestMsg, "厂商编码错误:" + note);
                        continue;
                    }
                    if (noteDetail[6] != "XY_DXHFInterface6E33YC")
                    {
                        CommonUtils.InsertMobileInterfaceLog("SMSAccept", strOriRequestMsg, "厂商密码错误:" + note);
                        continue;
                    }
                    // 获取发送短信记录ID
                    string sendID = InterfaceHelper.GetSMSSendID(smsReceive.ServiceNo);

                    // 获取发送短信明细ID
                    smsReceive.SendDetailID = InterfaceHelper.GetSMSSendDetailID(sendID, smsReceive.Mobile);

                    // 短信接收记录入库
                    InterfaceHelper.SaveSmsReceiveInfo(smsReceive);

                    // 修改发送短信明细表的是否回复状态
                    InterfaceHelper.SetSmsSendDetailIsReply(smsReceive.SendDetailID);

                    // 回收扩展码(全部回复才回收)
                    InterfaceHelper.CallBackExtNo(sendID);

                    SystemCode = noteDetail[5];
                    CommonUtils.InsertMobileInterfaceLog(SystemCode + "-SMSAccept", SerializeHelper.JsonSerializer<PartyMoSmsReceive>(smsReceive), strOriRequestMsg);
                }
                ResponseHelper.WriteResponseMessage(Response, "0");
            }
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(strOriRequestMsg))
                { strOriRequestMsg = ""; }

                CommonUtils.InsertMobileInterfaceLog("SMSAccept", strOriRequestMsg, ex.Message + ":" + ex.StackTrace);
                ResponseHelper.WriteResponseMessage(Response, "接收异常");
            }
        }
    }

    public class PartyMoSmsReceive
    {
        public string MoID { get; set; }
        public string ServiceNo { get; set; }
        public string Mobile { get; set; }
        public string RecContent { get; set; }
        public string SendDetailID { get; set; }
        public string RecTime { get; set; }
        public string SystemCode { get; set; }
        public string CryptograhpyKey { get; set; }
    }
}